/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.QuizDao;
import dao.QuizQuestionsDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Quiz;
import models.Quizquestion;

/**
 *  This servlet prepares the quiz question for the manage question page. it gets the questions out the database.
 * @author Vuong Pham<Vuong.Pham@hva.nl>
 */
public class QuizQuestionController extends HttpServlet {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //connection using Dao
        HibernateUtil hu = new HibernateUtil();
        QuizDao qd = new QuizDao(hu.openSession());
        QuizQuestionsDao qqd = new QuizQuestionsDao(hu.openSession());
        
        //gets the quiz
        Quiz quiz = qd.get(Long.parseLong(request.getParameter("quizid")));
        
        //gets the list of questions by quiz
        List<Quizquestion> qlist = qqd.findAllbyQuiz(quiz.getId());
        //if its not emty set attribute
        if (qlist.size() > 0) {
            request.setAttribute("questions", qlist);
        }

        request.setAttribute("quiz", quiz);

        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Quiz", "Manage quizzes"),
            new Breadcrumbs("QuizQuestion?quizid=" + quiz.getId(), "Manage questions")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/quiz_questions.jsp");
        dispatcher.forward(request, response);

    }
}
